package com.mooc.biz.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.mooc.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mooc.biz.CourseBiz;
import com.mooc.entity.Course;
import com.mooc.mapper.CourseMapper;
import com.mooc.util.UploadFile;
@Service(value="CourseBiz")
public class CourseBizImpl implements CourseBiz{

    SnowFlake snowFlake = new SnowFlake(2, 3);
	@Autowired
	CourseMapper courseMapper;
	@Override
	public List<Course> selectAllCourse() {
		// TODO Auto-generated method stub
		return courseMapper.selectAllCourse();
	}
	@Override
	public Course selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return courseMapper.selectByPrimaryKey(id);
	}
	@Override
	public int updateByPrimaryKeySelective(Course record) {
		// TODO Auto-generated method stub
		return courseMapper.updateByPrimaryKeySelective(record);
	}
	@Override
	public List<Course> coursesearch(String search) {
		// TODO Auto-generated method stub
		return courseMapper.coursesearch(search);
	}
	@Override
	public List<Course> freeCourse() {
		// TODO Auto-generated method stub
		return courseMapper.freecourse();
	}
	@Override
	public List<Course> vipCourse() {
		// TODO Auto-generated method stub
		return courseMapper.vipcourse();
	}
	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return courseMapper.deleteByPrimaryKey(id);
	}
	@Override
	public int savecourse(HttpServletRequest req) {
		// TODO Auto-generated method stub
        long l = snowFlake.nextId();
        Course course =  UploadFile.uploadFile(String.valueOf(l), req,Course.class);
        String video = course.getVideo();
        if (video != null) {
			course.setVideo("mp3/"+video+".gg");
		}
		if(course.getId()==null||0==course.getId()) {
			course.setId(l);
			courseMapper.insertSelective(course);
			/*System.out.println(course.getId());*/
		}else {
			courseMapper.updateByPrimaryKeySelective(course);
		}
		return 0;
	}

}
